package com.wuze.cloud.adapter;

import com.wuze.cloud.context.UserContext;
import com.wuze.cloud.dao.SysFolderDao;
import com.wuze.cloud.pojo.SysFolderDO;
import com.wuze.cloud.pojo.SysShareDO;
import com.wuze.cloud.pojo.SysUserDO;
import com.wuze.cloud.request.SysCreateShareQuery;
import com.wuze.cloud.response.SysShareVO;
import com.wuze.cloud.util.SnowFlakeIdWorker;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.wuze.cloud.enums.SysBooleanEnum.TRUE;

/**
 * @Author wuze
 * @Date 2023/5/8 19:19
 * @ClassName: SysShareAdapter
 * @Description: TODO
 */
@Component
public class SysShareAdapter {

    @Autowired
    private SysFolderDao folderDao;

    public SysShareVO buildSysShareVO(SysShareDO share){
        SysShareVO vo = new SysShareVO();
        BeanUtils.copyProperties(share,vo);
        SysFolderDO folder = folderDao.findById(share.getFolderId());
        vo.setFolderName(folder.getFolderName());
        return vo;
    }

    public List<SysShareVO> buildSysShareVOList(List<SysShareDO> shares){
        List<SysShareVO> res = Lists.newArrayList();
        shares.forEach(shareDO -> {
            res.add(buildSysShareVO(shareDO));
        });
        return res;
    }

    public SysShareDO buildSysShareDO(SysCreateShareQuery query) {
        SysUserDO user = UserContext.getUser();
        if (query.getForever().equals(TRUE.getCode())) {
            query.setStartDate(null);
            query.setDeadlineDate(null);
        }
        SysShareDO shareDO = new SysShareDO();
        shareDO.setId(SnowFlakeIdWorker.nextId());
        BeanUtils.copyProperties(query, shareDO);
        shareDO.setUserId(user.getId());
        shareDO.setUrl("");
        return shareDO;
    }
}
